<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col
        :span="24"
        :xs="24"
      >
        <el-form
          :model="queryParams"
          size="small"
          inline="true"
          label-width="68px"
        >
          <el-form-item label="商品名称">
            <el-input
              v-model="queryParams.productName"
              placeholder="请输入商品名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            >搜索</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-row gutter="10">
      <el-col span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['order:videoProduct:add']"
        >新增</el-button>
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :data="data"
    >
      <el-table-column
        label="商品ID"
        prop="id"
      ></el-table-column>
      <el-table-column
        label="商品名称"
        prop="productName"
      ></el-table-column>
      <el-table-column
        label="商品描述"
        prop="productDesc"
      ></el-table-column>
      <el-table-column
        label="文生视频次数"
        prop="textVideoCount"
      ></el-table-column>
      <el-table-column
        label="图生视频次数"
        prop="picVideoCount"
      ></el-table-column>
      <el-table-column
        label="限购次数"
        prop="limitNum"
      ></el-table-column>
      <el-table-column
        label="是否外部订单"
        prop="outOrder"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.outOrder == true">
            是
          </span>
          <span v-if="scope.row.outOrder == false">
            否
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="VIP等级"
        prop="vipLevel"
      ></el-table-column>
      <el-table-column
        label="会员时长(单位：天)"
        prop="vipTime"
      ></el-table-column>
      <el-table-column
        label="操作"
        width="100px"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
            v-hasPermi="['order:videoProduct:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleRemove(scope.row)"
            v-hasPermi="['order:videoProduct:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="handleQuery"
    />

    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="110px"
      >
        <el-form-item
          label="商品名称"
          prop="productName"
        >
          <el-input
            v-model="form.productName"
            placeholder="请输入商品名称"
          />
        </el-form-item>
        <el-form-item
          label="商品描述"
          prop="productDesc"
        >
          <el-input
            v-model="form.productDesc"
            placeholder="请输入商品描述"
          />
        </el-form-item>
        <el-form-item
          label="商品金额"
          prop="amount"
        >
          <el-input
            v-model="form.amount"
            placeholder="请输入商品金额"
          />
        </el-form-item>
        <el-form-item
          label="文生视频次数"
          prop="textVideoCount"
        >
          <el-input-number
            controls-position="right"
            v-model="form.textVideoCount"
            placeholder="请输入文生视频提问次数"
          />
        </el-form-item>
        <el-form-item
          label="图生视频次数"
          prop="picVideoCount"
        >
          <el-input-number
            controls-position="right"
            v-model="form.picVideoCount"
            placeholder="请输入图生视频提问次数"
          />
        </el-form-item>
        <el-form-item
          label="限购次数"
          prop="limitNum"
        >
          <el-input-number
            controls-position="right"
            v-model="form.limitNum"
            placeholder="请输入限购次数(0表示无限制)"
          />
        </el-form-item>
        <el-form-item
          label="是否外部订单"
          prop="outOrder"
        >
          <el-select
            v-model="form.outOrder"
            clearable
          >
            <el-option
              label="是"
              :value=true
            ></el-option>
            <el-option
              label="否"
              :value=false
            ></el-option>

          </el-select>
        </el-form-item>
        <el-form-item
          label="VIP等级"
          prop="vipLevel"
        >
          <el-input-number
            controls-position="right"
            v-model="form.vipLevel"
            placeholder="请输入VIP等级"
          />
        </el-form-item>
        <el-form-item
          label="会员时长(天)"
          prop="vipTime"
        >
          <el-input-number
            controls-position="right"
            v-model="form.vipTime"
            placeholder="请输入会员时长"
          />
        </el-form-item>

      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="submit()"
        >确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>

    </el-dialog>
  </div>
</template>

<script>
import { addProduct, getList, editProduct, deleteProduct } from '@/api/order/videoProduct.js'

export default {
  name: "videoProduct",
  data() {
    return {
      temp: undefined,
      title: undefined,
      open: false,
      loading: false,
      total: 0,
      queryParams: {
        productName: undefined,
        pageNum: 1,
        pageSize: 20
      },
      form: {},
      data: [],
      rules: {
        productName: [
          { required: true, message: "商品名不能为空", trigger: "blur" }
        ],
        productDesc: [
          { required: true, message: "商品描述不能为空", trigger: "blur" }
        ],
        amount: [
          { required: true, message: "商品金额不能为空", trigger: "blur" }
        ],
        textVideoCount: [
          { required: true, message: "文生视频提问次数不能为空", trigger: "blur" }
        ],
        picVideoCount: [
          { required: true, message: "图生视频提问次数不能为空", trigger: "blur" }
        ],
        outOrder: [
          { required: true, message: "是否外部订单不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.handleQuery()
  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    handleQuery() {
      this.loading = true;
      getList(this.queryParams).then(response => {
        this.data = response.data.data;
        this.total = response.data.total;
        this.loading = false;
      })
    },
    handleAdd() {
      this.reset()
      this.title = '新增视频商品'
      this.open = true
    },
    submit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            editProduct(this.form.id, this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            })
          }
          else {
            addProduct(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.handleQuery();
            })
          }
        }
      })
    },
    handleEdit(row) {
      this.form = row
      this.title = '编辑视频商品'
      this.open = true
    },
    handleRemove(row) {
      this.$modal.confirm('是否确认删除该商品').then(function () {
         return deleteProduct(row.id)
      }).then(() => {
        this.handleQuery();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });

    },
    // 表单重置
    reset() {
      this.form = {

      };
      this.resetForm("form");
    }
  }
}
</script>

<style lang="scss" scoped></style>